from django.db import models
from DjangoUeditor.models import UEditorField
from datetime import datetime
from django.contrib.auth.models import User
from django.utils import timezone
from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFit


# Create your models here.


class Category(models.Model):
    category = models.CharField(max_length=20, verbose_name='文章分类')

    class Meta:
        verbose_name = '文章分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category


class Article(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='文章所属分类')
    title = models.CharField(max_length=50, verbose_name='文章标题')
    body = UEditorField(verbose_name='文章正文', width=800, height=600, imagePath='blog/image/', filePath='blog/files',
                        default='')
    excerpt = models.CharField(verbose_name='文章摘要', max_length=200)
    created_time = models.DateTimeField(verbose_name='创建时间', default=timezone.now)
    # img = models.ImageField(verbose_name='封面图', upload_to='img/%Y/%m')
    img = ProcessedImageField(
        # 上传位置
        upload_to='article/%Y%m%d',
        # 处理规则,图片宽度和高度为420*280
        processors=[ResizeToFit(width=420, height=280)],
        # 存储格式
        format='JPEG',
        # 图片质量
        options={'quality': 100},
        verbose_name='文章缩略图'
    )
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者')
    updated = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')
    is_new = models.BooleanField(verbose_name='是否最新', default=True)

    class Meta:
        ordering = ('-created_time',)
        verbose_name = verbose_name_plural = '文章管理'

    def __str__(self):
        return self.title


class WebConfig(models.Model):
    banner_img = models.ImageField(verbose_name='封面图', upload_to='banner_img/%Y/%m')
    wechat = models.CharField(verbose_name='微信', max_length=20)
    cellphone = models.CharField(verbose_name='手机', max_length=20)
    email = models.EmailField(verbose_name='邮箱', max_length=50)
    sign = models.CharField(verbose_name='个人签名', max_length=50, default='')

    def __str__(self):
        return self.cellphone

    class Meta:
        verbose_name = verbose_name_plural = '网站配置'


# 访问网站的ip地址和次数
class UserIp(models.Model):
    ip = models.CharField(verbose_name='IP地址', max_length=30)  # ip地址
    count = models.IntegerField(verbose_name='访问次数', default=0)  # 该ip访问次数

    class Meta:
        verbose_name = '访问用户信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.ip


# 网站总访问次数
class VisitNumber(models.Model):
    count = models.IntegerField(verbose_name='网站访问总次数', default=0)  # 网站访问总次数

    class Meta:
        verbose_name = '网站访问总次数'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.count)


# 单日访问量统计
class DayNumber(models.Model):
    day = models.DateField(verbose_name='日期', default=timezone.now)
    count = models.IntegerField(verbose_name='网站访问次数', default=0)  # 网站访问总次数

    class Meta:
        verbose_name = '网站日访问量统计'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.day)
